package com.scsj.supermarket.model;

import com.scsj.supermarket.base.IndexBase;
import com.scsj.supermarket.bean.IndexBean;
import com.scsj.supermarket.bean.IndexMarketBean;
import com.scsj.supermarket.bean.LoginBean;

import java.util.Map;

import dkmvp.net.ResponseCallback;
import dkmvp.net.ResponseHandler;
import dkmvp.net.RetryWithDelay;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 *  首页除去店铺
 */

public class IndexModel extends BaseModel implements IndexBase.IModel {

    @Override
    public Subscription getData(RequestBody requestBody, final ResponseCallback<IndexBean> callback) {
        Observable<IndexBean> observable = service.GetIndex(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<IndexBean>() {
                    @Override
                    public void call(IndexBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }

    @Override
    public Subscription getIndexMarketData(RequestBody requestBody, final ResponseCallback<IndexMarketBean> callback) {

        Observable<IndexMarketBean> observable = service.GetIndexMarket(requestBody);

        Subscription sub = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .retryWhen(new RetryWithDelay(3, 3000))//总共重试3次，重试间隔3秒
                .subscribe(new Action1<IndexMarketBean>() {
                    @Override
                    public void call(IndexMarketBean result) {
                        callback.onSuccess(result);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        callback.onFaild(ResponseHandler.getErrorMsg(throwable));
                    }
                });
        return sub;
    }
}
